package com.oscar.ehome.utils.webview;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.oscar.ehome.R;

public class StatisticsWebView {

	Context context;
	WebView webView;

	public View addView(Context context, String url) {
		this.context = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getApplicationContext().getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.statistics_webview, null);
		webView = (WebView) view.findViewById(R.id.view_staticstics_webview);
		final ProgressBar progressBar = (ProgressBar) view
				.findViewById(R.id.view_staticstics_progressbar);
		progressBar.setVisibility(View.VISIBLE);
		// WebView加载web资源
		WebSettings webSettings = webView.getSettings();
		webSettings.setSavePassword(false);
		webSettings.setSaveFormData(true);// 保存表单数据
		webSettings.setJavaScriptEnabled(true);
		webSettings.setSupportZoom(true); // 不支持缩放
		webSettings.setBuiltInZoomControls(true);
		webSettings.setDefaultTextEncodingName("utf-8");
		webSettings.setRenderPriority(RenderPriority.HIGH);

		webSettings
				.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);// 排版适应屏幕
		webSettings.setUseWideViewPort(true);// 可任意比例缩放
		webSettings.setLoadWithOverviewMode(true);
		// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式ist.get(position);
		webView.setInitialScale(100);
		webView.setHorizontalScrollBarEnabled(false);// 水平不显示
		webView.setVerticalScrollBarEnabled(false); // 垂直不显示
		webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
		webView.loadUrl(url);
		webView.setWebChromeClient(new WebChromeClient() {

			public void onProgressChanged(WebView view, int progress) {
				progressBar.setProgress(progress * 100);
				if (progress == 100) {
					progressBar.setVisibility(View.GONE);
				}
			}
		});
		return view;
	}

}
